// pages/member/coupon/index.js
import {
    list
} from '../../../api/coupon'
import util from '../../../utils/util'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageNum:1,
        coupons: [],
        couponIndex: 0
    },
    formatDate(date) {
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        m = m < 10 ? '0' + m : m;
        var d = date.getDate();
        d = d < 10 ? ('0' + d) : d;
        // var h = date.getgetHours();
        // var m = date.getMinutes();
        // var s = date.getSeconds();
        return y + '-' + m + '-' + d;
    },

    loadList() {
        // var day1 = new Date("2017-9-17");
        // var day2 = new Date("2017-10-18");
        // console.log(day1+'---'+day2)
        // console.log((day2 - day1) / (1000 * 60 * 60 * 24));
        list({
            pageNum:this.data.pageNum,
            // sortField:'amount'   
        }).then(res => {
            res.data.data.forEach(item => {
                item.valid = new Date().getTime() < item.endDate
                var endDate= item.endDate
                item.endDate = util.datetimeFormat(item.endDate)
                var nowDate = new Date()
                var surplus=parseInt((endDate-nowDate)/1000/3600/24)
                item.surplus=surplus
                // if (item.targetType == 4) {
                //     this.data.coupons.push(item)
                // }
                this.data.coupons.push(item)
            })
            this.setData({
                coupons: this.data.coupons
            })
        })
    },


    onReachBottom: function () {
        this.data.pageNum+=1
        this.loadList()
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.loadList()
    },
    tabClick(e) {
        this.data.couponIndex = e.currentTarget.dataset.index
        this.setData({
            couponIndex: this.data.couponIndex
        })
    }
})